<template>
    <div class="bg">
        <input v-model="msg" :disabled="selectState"><button @click="get" :disabled="selectState">send</button>
        <p>{{ result }}</p>
        请遵守<a href="https://www.gov.cn/zhengce/zhengceku/202307/content_6891752.htm">《生成式人工智能服务管理暂行办法》</a>，不得提问违规违法问题
    </div>
</template>

<script setup>
import { ref } from "vue"
let msg = ref(""), result = ref(""), selectState = ref(false)
async function get() {
    if(!msg.value)return
    selectState.value = true
    result.value = "查询中"
    let data = await fetch("http://127.0.0.1:9025/AI/HH/HH?msg=" + msg.value)
    let r
    try {
        r = handle((await data.json()).result)
    } catch {
        result.value = "失败，请重试"
        selectState.value = false
        return
    }
    result.value = ""
    for (let f of r || []) {
        result.value += f
        await sleep()
    }
    selectState.value = false
}
function sleep() {
    return new Promise((resolve) => { setTimeout(resolve, Math.random() * 50) })
}
function handle(text) {
    return text.split("\n").filter(l => !!l && l.slice(0, 2) != 'id').map(l => JSON.parse(l.replace(/data:/, ''))).filter(l => l.data).map(l => l.data.split('data:')).flat().filter(l => !!l && !l.includes('[DONE]')).map(l => JSON.parse(l).choices[0].delta.content).filter(l => !!l)
}
</script>

<style scoped>
.bg {
    background: #ccc;
}
</style>